package org.surthi.app.bank.db.dao;


import java.util.Iterator;

import org.surthi.app.bank.db.dao.AccountHolderDao;
import org.surthi.app.bank.db.dvo.Account;
import org.surthi.app.bank.db.dvo.AccountHolder;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

public class AccountHolderDaoImpl extends HibernateDaoSupport implements
		AccountHolderDao {

	private TransactionTemplate transactionTemplate;
	
	public void getAllAccoundHolders() {
		
	}
	
	public void displayAccountHolderDetails(final String accountHolderId){
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				AccountHolder accountHolder = findAccountHolder(accountHolderId);
				System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
				System.out.println("Account Holder ID : "+ accountHolder.getAccountHolderId());
				System.out.println("Account Holder Name : "+accountHolder.getAccountHolderName());
				System.out.println("Account Holder Addr : "+accountHolder.getAccountHolderAddress());
				System.out.println("No'of accounts : "+ accountHolder.getAccounts().size());
				System.out.println("Account info : ");
				for (Iterator iter = accountHolder.getAccounts().iterator(); iter.hasNext(); ){
					Account account = (Account) iter.next();
					System.out.println("\t Acc No :"+account.getAccountId()+"\t Bank Name : "+account.getBank().getBankName());
				}
			}
		});
	}
	
	public AccountHolder findAccountHolder(final String accountHolderId){
		AccountHolder accountHolder = null;
		accountHolder = (AccountHolder) getHibernateTemplate().get(AccountHolder.class,accountHolderId);
		// System.out.println("No'of accounts : "+ accountHolder.getAccounts().size());
		return accountHolder;
	}
	public void saveAccountHolder(AccountHolder accountHolder) {
		getHibernateTemplate().save(accountHolder);
	}

	public void deleteAccountHolder(AccountHolder accountHolder) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(accountHolder);
	}

	/*public TransactionTemplate getTransactionTemplate() {
		return transactionTemplate;
	}*/

	public void setTransactionTemplate(PlatformTransactionManager transactionTemplate) {
		this.transactionTemplate = new TransactionTemplate(transactionTemplate);
	}
	
}
